using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace WIT.DAQ.Core.Forms.UserControls;

/// <summary>
///
/// </summary>
internal class UC_DeviceParams_For_OPCUA : UserControl
{
	/// <summary>
	/// OPCUA地址后缀（包括端口后的"/"符号）
	/// </summary>
	internal string SUFFIX = string.Empty;

	/// <summary>
	/// 是否需要使用OPCUA授权账号
	/// </summary>
	internal bool? NEEDACCOUNT = false;

	/// <summary>
	/// 账号
	/// </summary>
	internal string ACCOUNT = string.Empty;

	/// <summary>
	/// 密码
	/// </summary>
	internal string PWD = string.Empty;

	/// <summary> 
	/// 必需的设计器变量。
	/// </summary>
	private IContainer components = null;

	private Label label3;

	private Label label4;

	private TextBox txtAccount;

	private TextBox txtPwd;

	private CheckBox cbNeedAccount;

	private TextBox txtSuffix;

	private Label label1;

	private Label lblNotice;

	private Label label2;

	/// <summary>
	///
	/// </summary>
	internal UC_DeviceParams_For_OPCUA()
	{
		InitializeComponent();
	}

	/// <summary>
	///
	/// </summary>
	/// <param name="suffix"></param>
	/// <param name="needAccount"></param>
	/// <param name="account"></param>
	/// <param name="pwd"></param>
	internal UC_DeviceParams_For_OPCUA(string suffix, bool? needAccount, string account, string pwd)
	{
		InitializeComponent();
		SUFFIX = suffix;
		NEEDACCOUNT = needAccount;
		ACCOUNT = account;
		PWD = pwd;
	}

	private void UC_DeviceParams_For_OPCUA_Load(object sender, EventArgs e)
	{
		txtSuffix.Text = SUFFIX;
		cbNeedAccount.Checked = NEEDACCOUNT.HasValue && NEEDACCOUNT.Value;
		txtAccount.Text = ACCOUNT;
		txtPwd.Text = PWD;
	}

	private void cbNeedAccount_CheckedChanged(object sender, EventArgs e)
	{
		txtAccount.Enabled = cbNeedAccount.Checked;
		txtPwd.Enabled = cbNeedAccount.Checked;
	}

	/// <summary>
	/// 校验输入的内容是否合法
	/// </summary>
	/// <returns></returns>
	internal bool ValidateInputs(out string msg)
	{
		msg = string.Empty;
		bool flag = false;
		try
		{
			SUFFIX = txtSuffix.Text.Trim();
			if (cbNeedAccount.Checked)
			{
				if (string.IsNullOrEmpty(ACCOUNT))
				{
					throw new Exception("请填写OPCUA授权账号");
				}
				if (string.IsNullOrEmpty(PWD))
				{
					throw new Exception("请填写OPCUA授权密码");
				}
			}
			NEEDACCOUNT = cbNeedAccount.Checked;
			ACCOUNT = txtAccount.Text.Trim();
			PWD = txtPwd.Text.Trim();
			flag = true;
		}
		catch (Exception ex)
		{
			flag = false;
			msg = ex.Message;
		}
		return flag;
	}

	/// <summary> 
	/// 清理所有正在使用的资源。
	/// </summary>
	/// <param name="disposing">如果应释放托管资源，为 true；否则为 false。</param>
	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	/// <summary> 
	/// 设计器支持所需的方法 - 不要修改
	/// 使用代码编辑器修改此方法的内容。
	/// </summary>
	private void InitializeComponent()
	{
		this.label3 = new System.Windows.Forms.Label();
		this.label4 = new System.Windows.Forms.Label();
		this.txtAccount = new System.Windows.Forms.TextBox();
		this.txtPwd = new System.Windows.Forms.TextBox();
		this.cbNeedAccount = new System.Windows.Forms.CheckBox();
		this.txtSuffix = new System.Windows.Forms.TextBox();
		this.label1 = new System.Windows.Forms.Label();
		this.lblNotice = new System.Windows.Forms.Label();
		this.label2 = new System.Windows.Forms.Label();
		base.SuspendLayout();
		this.label3.AutoSize = true;
		this.label3.Location = new System.Drawing.Point(62, 82);
		this.label3.Name = "label3";
		this.label3.Size = new System.Drawing.Size(53, 12);
		this.label3.TabIndex = 23;
		this.label3.Text = "Account:";
		this.label4.AutoSize = true;
		this.label4.Location = new System.Drawing.Point(56, 109);
		this.label4.Name = "label4";
		this.label4.Size = new System.Drawing.Size(59, 12);
		this.label4.TabIndex = 22;
		this.label4.Text = "Password:";
		this.txtAccount.Enabled = false;
		this.txtAccount.Location = new System.Drawing.Point(121, 79);
		this.txtAccount.Name = "txtAccount";
		this.txtAccount.Size = new System.Drawing.Size(121, 21);
		this.txtAccount.TabIndex = 2;
		this.txtPwd.Enabled = false;
		this.txtPwd.Location = new System.Drawing.Point(121, 106);
		this.txtPwd.Name = "txtPwd";
		this.txtPwd.Size = new System.Drawing.Size(121, 21);
		this.txtPwd.TabIndex = 3;
		this.cbNeedAccount.AutoSize = true;
		this.cbNeedAccount.Location = new System.Drawing.Point(121, 57);
		this.cbNeedAccount.Name = "cbNeedAccount";
		this.cbNeedAccount.Size = new System.Drawing.Size(150, 16);
		this.cbNeedAccount.TabIndex = 1;
		this.cbNeedAccount.Text = "使用OPCUA授权账户登录";
		this.cbNeedAccount.UseVisualStyleBackColor = true;
		this.cbNeedAccount.CheckedChanged += new System.EventHandler(cbNeedAccount_CheckedChanged);
		this.txtSuffix.Location = new System.Drawing.Point(121, 30);
		this.txtSuffix.Name = "txtSuffix";
		this.txtSuffix.Size = new System.Drawing.Size(121, 21);
		this.txtSuffix.TabIndex = 0;
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(62, 33);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(53, 12);
		this.label1.TabIndex = 23;
		this.label1.Text = "Url后缀:";
		this.lblNotice.AutoSize = true;
		this.lblNotice.Font = new System.Drawing.Font("宋体", 9f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
		this.lblNotice.ForeColor = System.Drawing.SystemColors.ControlDark;
		this.lblNotice.Location = new System.Drawing.Point(248, 33);
		this.lblNotice.Name = "lblNotice";
		this.lblNotice.Size = new System.Drawing.Size(149, 12);
		this.lblNotice.TabIndex = 34;
		this.lblNotice.Text = "url例：opc.tcp://ip:port";
		this.label2.AutoSize = true;
		this.label2.Font = new System.Drawing.Font("宋体", 9f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 134);
		this.label2.ForeColor = System.Drawing.SystemColors.ControlText;
		this.label2.Location = new System.Drawing.Point(393, 33);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(54, 12);
		this.label2.TabIndex = 34;
		this.label2.Text = "/suffix";
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.label2);
		base.Controls.Add(this.lblNotice);
		base.Controls.Add(this.cbNeedAccount);
		base.Controls.Add(this.txtPwd);
		base.Controls.Add(this.txtSuffix);
		base.Controls.Add(this.txtAccount);
		base.Controls.Add(this.label4);
		base.Controls.Add(this.label1);
		base.Controls.Add(this.label3);
		base.Name = "UC_DeviceParams_For_OPCUA";
		base.Size = new System.Drawing.Size(523, 165);
		base.Load += new System.EventHandler(UC_DeviceParams_For_OPCUA_Load);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
